﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace StockMonitor.Configuration
{
    public class StockListConfig
    {
        #region Members.

        public static readonly StockListConfig Instance = new StockListConfig();

        #endregion

        #region Ctor.

        protected StockListConfig()
        {
        }

        #endregion

        #region Properties

        public string AllStockPath
        {
            get
            {
                return System.Windows.Forms.Application.StartupPath + System.Configuration.ConfigurationManager.AppSettings["AllStockPath"];
            }
        }

        private List<StockListItem> allStock = null;
        public List<StockListItem> AllStock
        {
            get
            {
                if (allStock == null)
                {
                    allStock = new List<StockListItem>();
                    foreach (var line in File.ReadAllLines(this.AllStockPath))
                    {
                        allStock.Add(new StockListItem()
                        {
                            StockCode = line.Trim().Substring(0, 6),
                            StockName = line
                        });
                    }
                }
                return allStock;
            }
        }

        #endregion
    }

    public class StockListItem
    {
        public string StockCode { get; set; }
        public string StockName { get; set; }

        public StockListItem() { }

        public StockListItem(string line)
        {
            int code;
            if (int.TryParse(line.Trim().Substring(0, 6), out code))
            {
                this.StockCode = line.Trim().Substring(0, 6);
                this.StockName = line.Substring(6).Trim();
            }
            else
            {
                throw new FormatException("配置行格式错误：" + line);
            }
        }
    }
}
